package com.beijing.cyou.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.beijing.cyou.model.Group;
import com.beijing.cyou.model.User;
import com.beijing.cyou.model.UserInfo;
import com.beijing.cyou.model.UserOnline;
import com.beijing.cyou.model.UserScore;
import com.beijing.cyou.module.DetailResult;
import com.beijing.cyou.params.UserParams;
import com.beijing.cyou.redis.UserRedisService;
import com.beijing.cyou.service.UserHelpService;
import com.beijing.cyou.service.UserService;
import com.beijing.cyou.utils.CommonConstant;
import com.beijing.cyou.utils.NotNullHelps;
import com.beijing.cyou.utils.UserHelpConstant;

@Slf4j
@RestController
@Api(value = "用户controller", description = "最核心API")
public class UserController {

	@Autowired
	private UserHelpService userHelpService;

	@Autowired
	private UserRedisService userRedisService;

	@Autowired
	private UserService userService ;
	
	private UserParams condition = new UserParams() ;

	private String message = "## 开发中！ @@";

	@ResponseBody
	@RequestMapping("/organization/member")
	@ApiOperation(value = "KM组织架构" , notes = "关联查询人员架构", httpMethod = "GET")
	public Group companyGroupMembers(@RequestParam(value = "idLevel" , required = false) Integer idLevel) {
		Long companyId = UserHelpConstant.COMPANY_ID ;
		UserParams condition = new UserParams();
		condition.setCompanyId(companyId);
		if (NotNullHelps.isNotNull(idLevel)) {
			condition.setIdLevel(idLevel);
		}
		Group group = userHelpService.getGroupInfo(condition);
		if (NotNullHelps.isNotNull(group) == false) {
			message = "&& 人员群--信息查询异常！ @@";
		} else {
			message = "))) 成功！ 查询人员群--信息成功 ((((";
		}
		log.info(message);
		return group;
	}

	@ResponseBody
	@RequestMapping(value = "/userScoreExp/{userId}/total", method = RequestMethod.GET)
	@ApiOperation(value = "用户积分经验总预览" , notes = "根据UseId查询", httpMethod = "GET")
	public UserScore getUserScoreExperience(@PathVariable Long userId) throws Exception {
		UserScore userScore = null;
		if (NotNullHelps.isNotNull(userId) == false) {
			message = CommonConstant.common_message_exception;
		} else {
			userScore = userRedisService.getUserScoreExpByUserId(userId);
			if (NotNullHelps.isNotNull(userScore) == false) {
				message = "%% 查询用户积分经验---异常 @@";
			} else {
				message = "## 成功查询用户积分经验。 @@ userId = " + userId;
			}
		}
		log.info(message);
		return userScore;
	}

	@ResponseBody
	@RequestMapping(value = "/userScoreExp/{userId}/total", method = RequestMethod.POST)
	@ApiOperation(value = "用户积分经验" , notes = "由UseId查询", httpMethod = "POST")
	public UserScore getUserScoreExp(@PathVariable Long userId) throws Exception {
		log.info("%%%% POST-- getUserScoreExp @@@@@");
		return getUserScoreExperience(userId);
	}

	@ResponseBody
	@RequestMapping("/userMsg")
	@ApiOperation(value = "注册/查询用户信息" , notes = "邮箱是关键", httpMethod = "GET")
	public UserInfo retriveUserInfo(@RequestParam(value = "token" , required = false) String token,
			@RequestParam(value = "username") String userName,String useremail, String domain, 
			String usergender, String usercomment)throws Exception {
		UserInfo userInfo = null;
		if (NotNullHelps.checkNotNull(userName, useremail, domain, usergender, usercomment) == false) {
			message = CommonConstant.common_message_exception;
		}else {
			condition = new UserParams(userName,useremail,domain,usergender,usercomment,token);
			userInfo = userService.retriveUserInfo(condition);
			if (NotNullHelps.isNotNull(userInfo) == false) {
				message = "&& 用户查询异常！ @@" ;
			}else {
				message = "** 查询用户成功！ @@ useremail = " + useremail ;
			}
		}
		log.info(message);
		return userInfo;
	}

	@ResponseBody
	@RequestMapping(value = "/userMsg", method = RequestMethod.POST)
	@ApiOperation(value = "注册/查询用户信息" , notes = "邮箱是关键", httpMethod = "POST")
	public UserInfo postUserMsg(@RequestParam(value = "token" , required = false) String token,
			String username, String useremail, String domain, String usergender, String usercomment)
					throws Exception {
		log.info("$$$$$ userMsg---post方法  @@@@@@");
		return retriveUserInfo(token,username, useremail, domain, usergender, usercomment);
	}

	@ResponseBody
	@RequestMapping("/user/{userId}")
	@ApiOperation(value = "查询用户" , notes = "由userId查询", httpMethod = "GET")
	public User getUserMessage(@RequestParam(value = "token" , required = false) String token ,
													@PathVariable Long userId)throws Exception{
		User user = null;
		if (NotNullHelps.checkNotNull(userId) == false) {
			message = CommonConstant.common_message_exception ;
		}else {
			UserParams condition = new UserParams(userId,token);
			user = userService.selectUser(condition);
			if (NotNullHelps.isNotNull(user)) {
				message = "&& token-userId 查询用户成功 @@";
			}else {
				message = "@@ 查询异常-userId-token ##";
			}
		}
		log.info(message);
		return user;
	}

	@ResponseBody
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/user/{userId}/update", method = RequestMethod.POST)
	@ApiOperation(value = "更新用户信息" , notes = "使用token校验", httpMethod = "POST")
	public DetailResult updateUserInfo(@RequestBody UserParams condition, @PathVariable Long userId,
										@RequestParam(value = "token" , required = false) String token ,
										HttpServletRequest request , HttpServletResponse response) throws Exception {
		DetailResult result = new DetailResult();
		String headerToken = request.getHeader("token");
		CommonConstant.setHttpPostNormalException(result, response);
		token = NotNullHelps.isNotNull(headerToken) ? headerToken : token ;
		UserOnline userOnline = userService.getRedisUser(token);
		if (NotNullHelps.isNotNull(userOnline) == false) {
			message = "$$ userRedis 查询异常！ @@" ;
		}else {
			Long userID = userOnline.getUserId();
			if (userId.equals(userID) == false) {
				message = "## token-userI 对应有误，异常 @@" ;
			}else {
				condition.setUserId(userID);
				boolean updateFlag = userService.updateUserInfo(condition) ;
				if (updateFlag == false) {
					message = "%% 更新 base-user 信息 没成功！ @@" ;
				}else {
					CommonConstant.setHttpPostSuccessStatusCode(result, response);
					message = "@@ 成功, (gender/phone/mobile/address)更新完毕 ##" ;
				}
			}
		}
		result.setMessage(message);
		log.info(message);
		return result;
	}

}
